﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using QzEbook.Common;
using System.Collections.Generic;
using System.Data.Common;

namespace QzEbook.AccessObject
{
    
    public class UserLevel : abstractAccess<UserLevel>
    {
        public static readonly string Member = "Member";

        private int level;

        public int Level
        {
            get { return level; }
            set { level = value; }
        }
        private int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private String name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }


        public UserLevel()
        {
        }

        protected override UserLevel bindRow(DataRow row)
        {
            UserLevel userLevel = new UserLevel();
            userLevel.Id = (Int32)row["LID"];
            userLevel.Name = row["LName"].ToString().Trim();
            userLevel.Level = (Int32)row["LLevel"];
            return userLevel;
        }

        /// <summary>
        /// 选取并缓存起来;
        /// </summary>
        /// <returns></returns>
        public new List<UserLevel> selectList()
        {
            if (HttpContext.Current.Cache["LevelList"] == null)
            {
                List<UserLevel> list=base.selectList();
                HttpContext.Current.Cache.Insert("LevelList", list, null, DateTime.Now.AddHours(1), TimeSpan.Zero);
            }
            return HttpContext.Current.Cache["LevelList"] as List<UserLevel>;
        }

        /// <summary>
        /// 取得等级名称;没有实现
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string getName(int levelId)
        {
            throw new NotImplementedException();
        }
        public static Int32 getLevelByName(string name)
        {
            List<UserLevel> list = new UserLevel().selectList();
            foreach (UserLevel item in list)
            {
                if (item.Name == name)
                {
                    return item.Level;
                }
            }
            return -1;
        }
        protected override DataTable selectTable()
        {
            DbCommand command = CreateCommand("SelectLevel");
            return DataAccess.ExcuteSelectCommand(command);
        }
    }
}